/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.modules.project.dao.ModuleDao;
import io.renren.modules.project.entity.ModuleEntity;
import io.renren.modules.project.service.ModuleService;
import io.renren.modules.systemconfig.dao.SysConfigServerDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@Service("moduleService")
public class ModuleServiceImpl extends ServiceImpl<ModuleDao, ModuleEntity> implements ModuleService {
	@Autowired
	private SysConfigServerDao serverMapper;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String key = (String)params.get("key");
		Long  current = Long.parseLong((String)params.get("page"));
		Long size = Long.parseLong((String)params.get("limit"));
		List<ModuleEntity> records = this.baseMapper.selectEntityPage(key,size*(current-1),size*current);
		IPage<ModuleEntity> page = new Page<>(current, size, this.baseMapper.selectRecordSize(key));
		page.setRecords(records);
		return new PageUtils(page);
	}
	@Override
	public ModuleEntity getAllInfoById(long moduleId) {
		return baseMapper.getAllById(moduleId);
	}

	@Override
	public List<HashMap> selectModuleDatasourceListByProjectIdAndDatasourceId(long projectId, long id) {
		return this.baseMapper.selectModuleDatasourceListByProjectIdAndDatasourceId(projectId,id);
	}

	@Override
	public List<ModuleEntity> getListByServerId(Long id) {
		return this.baseMapper.selectList(new QueryWrapper<ModuleEntity>().eq("server_id",id));
	}

	@Override
	public ModuleEntity getModuleServerById(Long moduleId) {
		ModuleEntity me = this.baseMapper.selectById(moduleId);
		if(me!=null&&me.getServerId()!=null){
			me.setServer(serverMapper.selectById(me.getServerId()));
		}
		return me;
	}

	@Override
	public List<ModuleEntity> selectModuleServerListByBackendId(Long backend_id) {
		List<ModuleEntity> list = this.baseMapper.selectList(new QueryWrapper<ModuleEntity>().eq("backend_id",backend_id));
		list.stream().forEach(e->{
			e.setServer(serverMapper.selectById(e.getServerId()));
		});
		return list;
	}

	@Override
	public ModuleEntity queryByKey(String key) {
		return baseMapper.queryByKey(key);
	}

	@Override
	@Transactional
	public void saveEntity(ModuleEntity entity) {
		this.baseMapper.insert(entity);
	}

	@Override
	@Transactional
	public void update(ModuleEntity user) {
		this.updateById(user);
	}

	@Override
	public void deleteBatch(Long[] userId) {
		this.removeByIds(Arrays.asList(userId));
	}

}